package com.android.car_bluetooth_audio.ble;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import androidx.annotation.NonNull;

public class VibrationBleManager extends BleManager<VibrationManagerCallbacks>{

    private static final String TAG = "VibrationBleManager";

    public final static UUID SERVICE_UUID = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");
    private static final UUID MEASUREMENT_CHARACTERISTIC_UUID = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb");
    //    private static final UUID MEASUREMENT_CHARACTERISTIC_UUID1 = UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
    private BluetoothGattCharacteristic mVibrationCharacteristic;
//    private BluetoothGattCharacteristic mReceivedVibrationCharacteristic;


    public VibrationBleManager(@NonNull Context context) {
        super(context);
    }
    private Timer mTestTimer;
    private StringBuilder stringBuilder = new StringBuilder();
    private String sendModel ="";
    private int off;
    private int type;
    private ArrayList<String> recData = new ArrayList<>();

    @NonNull
    @Override
    protected BleManager<VibrationManagerCallbacks>.BleManagerGattCallback getGattCallback() {
        return mGattCallback;
    }

    private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {

        @Override
        protected Deque<Request> initGatt(final BluetoothGatt gatt) {
            final LinkedList<Request> requests = new LinkedList<>();
            requests.add(Request.newEnableIndicationsRequest(mVibrationCharacteristic));
//            requests.add(Request.newEnableNotificationsRequest(mReceivedVibrationCharacteristic));
//            requests.add(Request.newReadRequest(mReceivedVibrationCharacteristic));
            return requests;
        }

        @Override
        protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
//			BluetoothConstant E2:72:CE:3F:98:2E
            Log.e("连接的参数", String.valueOf(SERVICE_UUID));
            final BluetoothGattService service = gatt.getService(SERVICE_UUID);
            if (service != null) {
                Log.e("service不为空", String.valueOf(service.getType()));
                List<BluetoothGattCharacteristic> gattCharacteristics =   service.getCharacteristics();
                for (int i = 0;i<gattCharacteristics.size();i++){
                    BluetoothGattCharacteristic c1 = gattCharacteristics.get(i);
                    Log.e("c1巅峰",c1.getUuid().toString());
                }
                mVibrationCharacteristic = service.getCharacteristic(MEASUREMENT_CHARACTERISTIC_UUID);
//                mReceivedVibrationCharacteristic = service.getCharacteristic(MEASUREMENT_CHARACTERISTIC_UUID);
            }else {
                Log.e("service为空","退出");
            }
            return mVibrationCharacteristic != null;
        }

        @Override
        protected void onDeviceDisconnected() {
            mVibrationCharacteristic = null;
//            mReceivedVibrationCharacteristic=null;

        }

        @Override
        protected void onCharacteristicNotified(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            Log.e("onNotified",characteristic.getUuid().toString());
            if (characteristic.getUuid().equals(MEASUREMENT_CHARACTERISTIC_UUID)){

                byte[] value = characteristic.getValue();
                String data = ParserUtils.bytesToHexString(value);
                Log.e("蓝牙的string不能活",data);
                Log.e("sendmodel",sendModel);
                Log.e("type是锁定", String.valueOf(type));

                if (sendModel.equalsIgnoreCase("syncData")){
                    Log.e("进入这","手动");
                    getSyscData(data);
                }else if (sendModel.equalsIgnoreCase("switchInfo")){
                    switchInfo(data);
                }else if (sendModel.equalsIgnoreCase("EQMode")){
                    eqMode(data);
                    sendModel = "";
                }else if (sendModel.equalsIgnoreCase("Volum")){

                }else if (sendModel.equalsIgnoreCase("Save")){
                    saveMode(data);

                }else if (sendModel.equalsIgnoreCase("Boost")){

                }else if (sendModel.equalsIgnoreCase("Rest")){
                    resetMode(data);
                    sendModel="";
                }else if (sendModel.equalsIgnoreCase("Slide")){

                }else if (sendModel.equalsIgnoreCase("0Or180Data")){
                    result0Or180(data);
                }



//                if (sendModel.equals("ChannelSelectInfo")){
//                    mCallbacks.onChannelSelectSuccess(type);
//                }else if (sendModel.equals("UpdateDataToBlueTooth")){
//                    mCallbacks.onUpdateDataToBlueTooth(type);
//                }else if (sendModel.equals("ReadTimeZoneStatus")){
//
//                    mCallbacks.ReadTimeZoneStatus(value,type);
//                }else if (sendModel.equals("Read2048_0")){
//                    read2048Data(data,0,off);
////                    mCallbacks.Read2048_0(value,off);
//                }else if (sendModel.equals("Read2048_1")){
//                    read2048Data(data,1,off);
////                    mCallbacks.Read2048_1(value,off);
//                }else if (sendModel.equals("Read2048_2")){
//                    read2048Data(data,2,off);
////                    mCallbacks.Read2048_2(value,off);
//                }else if (sendModel.equals("Power")){
//                    Log.e("获取到的power",data);//0104020064b8db
//                    mCallbacks.getPowerData(value,type);
//                }else if (sendModel.equals("DeviceData")){
//                    Log.e("获取到的deviceData",data);
//                    getDeviceData(data);
////                    mCallbacks.getDeviceData(value);
//                }else if (sendModel.equals("DeviceHZData")){
//                    mCallbacks.getDeviceHzData(value);
//                }else if (sendModel.equals("DeviceDataWrite")){
//                    mCallbacks.deviceDataWrite(value);
//                }else if (sendModel.equals("DeviceDataSubmit")){
//                    mCallbacks.deviceDataSubmit(value);
//                }else if (sendModel.equals("CharacteristicData")){
//                    getCharacteristic(gatt, data);
//                }
            }

        }

        @Override
        public void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
            Log.e("onIndicated",characteristic.getUuid().toString());


        }


    };




    //    //获取设备信息数据
//    private void getDeviceData(String data) {
//        stringBuilder.append(data);
//        Log.e("string",stringBuilder.toString());
//        if(mTestTimer!=null)
//        {
//            mTestTimer.cancel();
//            mTestTimer.purge();
//        }
//        mTestTimer = new Timer();
//        mTestTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
////                        Log.d(TAG, "Timeout Sent Message: "+System.currentTimeMillis());
////                        Message mes = new Message();
////                        mes.what = 1;
////                        mHandler.sendMessage(mes);
//                String value = stringBuilder.toString();
//                String n = value.substring(6,value.length()-4);
//                byte[] b = ParserUtils.hexStringToBytes(n);
//                String name = ParserUtils.parse(b);
//                Log.e("valueoppo的长度", String.valueOf(b.length));
//                Log.e("valueoppo",value);
//                Log.e("valueopp44o",name);
//                Log.e("valueopp44o",n);
//                mCallbacks.getDeviceData(b);
//                stringBuilder.delete(0,stringBuilder.length());
//            }
//        },500);
//
//
//    }
//
//    private void getCharacteristic(BluetoothGatt gatt, String data) {
//        stringBuilder.append(data);
//        Log.e("string",stringBuilder.toString());
//        if(mTestTimer!=null)
//        {
//            mTestTimer.cancel();
//            mTestTimer.purge();
//        }
//        mTestTimer = new Timer();
//        mTestTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
////                        Log.d(TAG, "Timeout Sent Message: "+System.currentTimeMillis());
////                        Message mes = new Message();
////                        mes.what = 1;
////                        mHandler.sendMessage(mes);
//                String value = stringBuilder.toString();
//                String n = value.substring(value.length()-96,value.length()-4);
//                byte[] b = ParserUtils.hexStringToBytes(n);
//                String name = ParserUtils.parse(b);
//                Log.e("valueoppoC",value);
//                Log.e("valueopp44oC",name);
//                Log.e("valueopp44oC",n);
//
//                mCallbacks.onValueReceived(gatt.getDevice(),b,type);
//                stringBuilder.delete(0,stringBuilder.length());
//            }
//        },500);
//    }
//
//    public void read2048Data(String data,int index,int off){
//
//        stringBuilder.append(data);
//        Log.e("string",stringBuilder.toString());
//        if(mTestTimer!=null)
//        {
//            mTestTimer.cancel();
//            mTestTimer.purge();
//        }
//        mTestTimer = new Timer();
//        mTestTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
////                        Log.d(TAG, "Timeout Sent Message: "+System.currentTimeMillis());
////                        Message mes = new Message();
////                        mes.what = 1;
////                        mHandler.sendMessage(mes);
//                String value = stringBuilder.toString();
//                String n = value.substring(6,value.length()-4);
//                byte[] b = ParserUtils.hexStringToBytes(n);
//                String name = ParserUtils.parse(b);
//                if (index==0){
//                    mCallbacks.Read2048_0(b,off,type);
//                }else if (index==1){
//                    mCallbacks.Read2048_1(b,off,type);
//                }else if (index==2){
//                    mCallbacks.Read2048_2(b,off,type);
//                }
//                stringBuilder.delete(0,stringBuilder.length());
//            }
//        },500);
//
//
//    }
//获取开关信息
    private void switchInfo(String data) {

        mCallbacks.switchData(data);

    }

    private void eqMode(String data) {

        mCallbacks.eqMode(data);

    }

    //结果
    private void result0Or180(String data) {
        Log.e("进入这里了","商机豆");
        mCallbacks.result0Or180(data);
    }

    private void resetMode(String data) {
        Log.e("设置模式",data);
        mCallbacks.resetMode(data);
    }

    private void saveMode(String data) {
        Log.e("设置模式",data);
        mCallbacks.saveMode(data);
    }

    //获取同步数据
    private void getSyscData(String data) {

        recData.add(data);
        if(mTestTimer!=null)
        {
            mTestTimer.cancel();
            mTestTimer.purge();
        }
        mTestTimer = new Timer();
        mTestTimer.schedule(new TimerTask() {
            @Override
            public void run() {
//                        Log.d(TAG, "Timeout Sent Message: "+System.currentTimeMillis());
//                        Message mes = new Message();
//                        mes.what = 1;
//                        mHandler.sendMessage(mes);
//                String value = stringBuilder.toString();
//                String n = value.substring(6,value.length()-4);
//                byte[] b = ParserUtils.hexStringToBytes(n);
//                String name = ParserUtils.parse(b);
//                Log.e("valueoppo的长度", String.valueOf(b.length));
//                Log.e("valueoppo",value);
//                Log.e("valueopp44o",name);
//                Log.e("valueopp4
                String[] d = new String[recData.size()];
                String[] datas = recData.toArray(d);
                Log.e("valueopp44osd", String.valueOf(datas.length));
                mCallbacks.syscData(datas);
                recData.clear();
            }
        },1000);


    }


    public void sendCharacteristicData(byte[] data,int type) {
        Log.e("是对吗","山东网");
        if (mVibrationCharacteristic == null)
            return;
//        mCallbacks.onOperationStarted(mBluetoothDevice);
        Log.e("是怎样的","山东网好的");
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel = "CharacteristicData";
        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void sendSwitch(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "SwitchInfo";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void syncData(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "syncData";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void send0Or180(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "0Or180Data";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void sendEQMode(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "EQMode";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void sendVolum(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "Volum";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void sendSave(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "Save";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void sendBoost(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "Boost";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void sendRest(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "Rest";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }

    public void sendSlide(byte[] data){
        if (mVibrationCharacteristic == null)
            return;
        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
        characteristic.setValue(data);
        sendModel  = "Slide";
//        this.type = type;
//        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
        writeCharacteristic(characteristic);
    }


//    public void sendChannelSelectInfo(byte[] data,int type){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "ChannelSelectInfo";
//        this.type = type;
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//
//
//    }
//
//    public void updateDataToBlueTooth(byte[] data,int type){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "UpdateDataToBlueTooth";
//        this.type = type;
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//
//
//
//    }
//
//    public void  read2048_0(byte[] data,int off0,int type){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "Read2048_0";
//        off = off0;
//        this.type = type;
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
//
//    public void  read2048_1(byte[] data,int off1,int type){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "Read2048_1";
//        off = off1;
//        this.type = type;
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
//
//    public void  read2048_2(byte[] data,int off2,int type){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "Read2048_2";
//        off = off2;
//        this.type = type;
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
//
//    public void getPower(byte[] data,int type){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "Power";
//        this.type = type;
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
//
//    public void getBlueToothData(byte[] data){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "DeviceData";
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
//
//    public void deviceDataWrite(byte[] data){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "DeviceDataWrite";
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
//
//    public void deviceDataSubmit(byte[] data){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "DeviceDataSubmit";
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
//
//    public void getBlueToothHZData(byte[] data){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "DeviceHZData";
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
//
//    public void readTimeZoneStatusForBlueTooth(byte[] data,int type){
//        if (mVibrationCharacteristic == null)
//            return;
//        final BluetoothGattCharacteristic characteristic = mVibrationCharacteristic;
//        characteristic.setValue(data);
//        sendModel  = "ReadTimeZoneStatus";
////        setOpCode(characteristic, OP_CODE_REPORT_STORED_RECORDS, OPERATOR_FIRST_RECORD);
//        writeCharacteristic(characteristic);
//    }
}
